package com.ruoyi.monitor.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.monitor.Vo.PredictDataVo;
import com.ruoyi.monitor.domain.Pollutant;
import com.ruoyi.monitor.service.IPollutantService;
import com.ruoyi.monitor.utils.Common.DateUtils;
import com.ruoyi.monitor.utils.PythonServer.PyConnect;
import com.ruoyi.monitor.utils.PythonServer.PyConstant;
import com.ruoyi.monitor.utils.PythonServer.predict.PreUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.monitor.domain.PreModel;
import com.ruoyi.monitor.service.IPreModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模型预测Controller
 * 
 * @author xzx
 * @date 2023-04-22
 */
@RestController
@RequestMapping("/monitor/pre_model")
public class PreModelController extends BaseController
{
    @Autowired
    private IPreModelService preModelService;
    @Autowired
    private PyConnect pyConnect;
    @Autowired
    private PreUtils preUtils;
    @Autowired
    private IPollutantService pollutantService;
    @Autowired
    @Qualifier("threadPoolTaskExecutor")
    ThreadPoolTaskExecutor pool;
    @Autowired
    private RedisCache redisCache;
    private static Logger logger = Logger.getLogger(PollutantController.class);

    /**
     * 预测 3个月单项污染物
     * @param type
     * @return
     */
    @PostMapping(value = "/pre/single")
    public AjaxResult getSinglePre(@RequestBody String type ){
        System.out.println(type);
        Map<String, Object> map = redisCache.getCacheMap(type);
        if (ObjectUtil.isEmpty(map)) {
            preUtils.getForPredictOne();
            map = redisCache.getCacheMap(type);
        }
        return success(new TreeMap<>(map));
    }

    /**
     * 预测3个月aqi
     * @return
     */
    @GetMapping(value = "/pre/3month")
    public AjaxResult getPredictFor3Month(){
        Map<String, Object> map = null;
        map = redisCache.getCacheMap("aqi");
        if (ObjectUtils.isEmpty(map)){
            preUtils.getForPredictOne();
            map = redisCache.getCacheMap("aqi");
//            redisCache.expire("preMonth30*3",8, TimeUnit.HOURS);
        }
        return success(new TreeMap<>(map));
    }

    /**
     * 预测一个月内的数据
     * @return
     */
    @GetMapping(value = "/pre/month")
    public AjaxResult getPredictForMonth(){
        Map<String, Object> map = null;
        //可变更预测天数
        preUtils.predictForMonth(24*30);
        map = redisCache.getCacheMap("preMonth30");
        return success(map);
    }

    /**
     * 预测24小时内的数据
     * @return
     */
    @GetMapping(value = "/pre/24")
    public AjaxResult getPredictForDaily() {
        PredictDataVo res = null;
        res = redisCache.getCacheObject("pre24");
        System.out.println(res);
        if (ObjectUtils.isEmpty(res)){
            preUtils.predictForDay();
        }
        return success(res);

    }

        /**
         * 查询模型预测列表
         */
    @PreAuthorize("@ss.hasPermi('monitor:pre_model:list')")
    @GetMapping("/list")
    public TableDataInfo list(PreModel preModel)
    {
        startPage();
        List<PreModel> list = preModelService.selectPreModelList(preModel);
        return getDataTable(list);
    }

    /**
     * 导出模型预测列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:pre_model:export')")
    @Log(title = "模型预测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreModel preModel)
    {
        List<PreModel> list = preModelService.selectPreModelList(preModel);
        ExcelUtil<PreModel> util = new ExcelUtil<PreModel>(PreModel.class);
        util.exportExcel(response, list, "模型预测数据");
    }

    /**
     * 获取模型预测详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:pre_model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(preModelService.selectPreModelById(id));
    }

    /**
     * 新增模型预测
     */
    @PreAuthorize("@ss.hasPermi('monitor:pre_model:add')")
    @Log(title = "模型预测", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreModel preModel)
    {
        return toAjax(preModelService.insertPreModel(preModel));
    }

    /**
     * 修改模型预测
     */
    @PreAuthorize("@ss.hasPermi('monitor:pre_model:edit')")
    @Log(title = "模型预测", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreModel preModel)
    {
        return toAjax(preModelService.updatePreModel(preModel));
    }

    /**
     * 删除模型预测
     */
    @PreAuthorize("@ss.hasPermi('monitor:pre_model:remove')")
    @Log(title = "模型预测", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(preModelService.deletePreModelByIds(ids));
    }
}
